<template>
	<view :style="{'background-color': (!isShowKeywordList ? '#fff' : '')}">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<icon type="search" :size='13' color='#333'></icon>
				<input  placeholder-class="tui-input-plholder" class="tui-input" confirm-type="search" :adjust-position="true" placeholder="搜索驿站" v-model="keyword" @confirm="doSearch(keyword)" />
				<icon type="clear" :size='13' color='#bcbcbc' @click.stop="clear" v-show="keyword"></icon>
			</view>
			<view class="tui-cancle" @tap="doSearch(keyword)">搜索</view>
		</view>
		<view class="search-order">
			<view v-if="isShowKeywordList" class="lists">
				<view class="add-info">
					<view class="itme bg-white u-f" v-for="(item,index) in postList" :key="index">
						<image :src="item.door_photo" mode="aspectFill" class="post-img"></image>
						<view class="u-fc flex1">
							<view class="u-f-jsb" style="align-items: flex-start;">
								<view class="u-fc title">
									<view>{{item.pick_name}}</view>
									<view class="tui-por-del">
										<view class="del-item del-item-blue">支持冷藏</view>
										<view class="del-item del-item-red">优质服务</view>
									</view>
								</view>
								<view class="round bg-blue text-sm" v-if="parseInt(item.open_hours.replace(':',''))<=curTime && parseInt(item.end_hours.replace(':',''))>=curTime" @click="choosePost(item)">选择驿站</view>
								<view class="round bg-gray text-sm" v-else>暂停接单</view>
							</view>
							
							<view class="distance">距您：{{item.distance_um | dist}}</view>
							<view class="det text-m">{{item.pick_addr}}</view>
							<view class="u-f-jsb">
								<view class="u-f-ajc time"><text class="icon iconfont icon-shijian"></text>营业时间：{{item.open_hours}} - {{item.end_hours}}</view>
								<!-- <view class="u-f-ajc cont" @tap="call(item.phone)"><text class="icon iconfont icon-dianhua"></text>联系商家</view> -->
							</view>
						</view>
						
					</view>
				</view>
				<view>
					<loadmore :visible="loadMore"></loadmore>
					<om-nomore :visible="noMoreFlag"></om-nomore>
					<tui-no-data imgUrl="https://zj.gdwrh.com/fnyg/static/images/my-img/img_nodata.png" v-if="noData" :fixed="true">该区域尚未开发，敬请期待</tui-no-data>
				</view>
			</view>
			<view v-else class="record">
				<view class="margin-lr" v-if="oldKeywordList.length>0">
					<view class="tui-history-header">
						<view class="tui-search-title">历史搜索</view>
						<view class="tui-icon-delete" @tap="oldDelete()">删除</view>
					</view>
					<view class="tui-history-content">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index" class="tui-circle round">{{keyword}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import cache from '@/common/js/utils/cache.js'
	import gcoord from 'gcoord'
	export default {
		data() {
			return {
				curTime:0,
				isShowKeywordList: false,
				oldKeywordList:[],
				currentPage: 1,
				total: 0,
				loadMore: false,
				noMoreFlag: false,
				noData: false,
				keyword:'',
				postList: [],
				longitude:'',
				latitude:''
			}
		},
		onLoad() {
			const mydate = new Date()
			let hh = mydate.getHours()
			let mm = (mydate.getMinutes()+'').padStart(2,'0')
			// 获取当前时间，用来判断驿站是否营业
			this.curTime = parseInt(`${hh}${mm}`);''
			this.loadOldKeyword()
			let that = this
			// #ifdef H5
			that.longitude = 23
			that.latitude = 110
			// #endif
			// 获取本地经纬度
			uni.getLocation({
				type: 'wgs84',
				geocode: true,
				success(e) {
					// 经纬度处理
					var result = gcoord.transform(
					  [e.longitude, e.latitude],    // 经纬度坐标
					  gcoord.WGS84,               // 当前坐标系
					  gcoord.BD09 
					);
					that.longitude = result[0]
					that.latitude = result[1]
					// 业务处理
				},
				fail(e) {
					that.$tip.toast("定位失败")
				}
			})
		},
		methods:{
			getPost(pickName) {
				this.loadMore = true;
				this.$http.post('/api/auth/member/chief/list', {pickName,currentPage:this.currentPage,longitude:this.longitude,latitude:this.latitude},{ header: { authType: 'false'}}).then(res => {
					this.loadMore = false;
					this.total = res.total;
					if (this.total == 0) return (this.noData = true);
					this.postList = [...this.postList, ...res.rows];
				});
			},
			//执行搜索
			doSearch(keyword) {
				keyword = keyword === '' ? this.defaultKeyword : keyword;
				this.isShowKeywordList = true;
				this.keyword = keyword;
				this.save()
				this.getPost(keyword);
			},
			// 以下是搜索所有内容!!!!
			clear: function() {
				uni.hideKeyboard();
				this.keyword = '';
				this.isShowKeywordList = false;
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'postSearch',
					success: res => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: res => {
						if (res.confirm) {
							// 用户点击确定
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'postSearch'
							});
						} else if (res.cancel) {
							// 用户点击取消
						}
					}
				});
			},
			save() {
				this.keyword ? this.saveKeyword(this.keyword) : '';
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				let that = this
				cache.putByLimit('postSearch',keyword,function(res){
					that.oldKeywordList =  res
				})
			},
			choosePost(item){
				this.Route({type:'back',delta:2})
				cache.put('pickUpPoint',item)
			}
		}
		
	}
</script>

<style lang="scss">
	page {
		color: #333;
		background: #FFFFFF;
		min-height: 100vh;

	}
	.search-order {
		padding-top: 44px;
	}
	.lists {
		padding: 20rpx;
	}
	.baseW {
		// border-bottom: 1px solid #ccc;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}
	.tui-searchbox {
		padding: 10rpx 28rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		background-color: #FFFFFF;
		position: fixed;
		width: 100%;
		z-index: 999;
	}
	
	.tui-search-input {
		width: 100%;
		height: 66rpx;
		border-radius: 35rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}
	
	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16rpx;
		font-size: 28rpx;
	}
	
	.tui-input-plholder {
		font-size: 28rpx;
		color: #b2b2b2;
	}
	
	.tui-cancle {
		color: #888;
		font-size: 28rpx;
		padding-left: 30rpx;
		flex-shrink: 0;
	}
	/* 历史搜索 */
	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
	}
	.tui-search-title {
		font-size: 28rpx;
		font-weight: bold;
	}
	.tui-icon-delete {
		padding: 10rpx;
	}
	.tui-history-content{
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}
	.tui-circle{
		background-color: #f2f2f2;
		padding: 14upx 20upx;
		margin: 0 20upx 20upx 0;
	}
	.keyword-entry-tap {
		background-color:#F1F1F1;
	}
	// 列表
	.add-info{
		width: 100%;
		& > scroll-view{
			height: 500upx;
		}
		.itme{
			border-radius: 20upx;
			padding: 30upx 28upx;
			margin-bottom: 20upx;
			.title{
				font-size: 30upx;
				font-weight: bold;
				color: #080808;
				.icon{
					color: $main-color;
				}
			}
			.post-img{
				border-radius: 18rpx;
				width: 166rpx;
				height: 170rpx;
				flex-shrink: 0;
				display: block;
				margin-right: 18rpx;
			}
			.distance{
				font-size: 24upx;
			}
			.det{
				// width: 430upx;
				margin: 10upx 0;
			}
			.round{
				flex-shrink: 0;
				padding: 10rpx 20rpx;
				margin-left: 18rpx;
			}
			.time{
				.icon{
					color: #080808;
					font-size: 40upx;
				}
			}
			.cont{
				color: $main-color;
				.icon{
					font-size: 40upx;
				}
			}
		}
	}
</style>
